﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
using RapidWebDev.SearchIndex.Contract;

namespace RapidWebDev.SearchIndex.DocumentFields
{
	/// <summary>
	/// Interface to access indexing document field.
	/// </summary>
	internal abstract class BaseDocumentField : IDocumentField
	{
		/// <summary>
		/// Indexing document field type.
		/// </summary>
		public abstract IndexDocFieldValueTypes FieldType { get; }

		/// <summary>
		/// Convert indexing document field message to lucene fields.
		/// </summary>
		/// <param name="indexDocField"></param>
		/// <returns></returns>
		public abstract Fieldable[] GetLuceneFields(IndexDocFieldMessage indexDocField);

		/// <summary>
		/// Resolve original field value from Lucene stored field value.
		/// </summary>
		/// <param name="luceneStoredFieldValue"></param>
		/// <returns></returns>
		public abstract string ResolveOriginalFieldValue(string luceneStoredFieldValue);

		/// <summary>
		/// Append the search filter to the query to run.
		/// </summary>
		/// <param name="queryToRun"></param>
		/// <param name="searchFilterMessage"></param>
		/// <param name="stemmingLanguage"></param>
		/// <returns></returns>
		public void AppendQuery(BooleanQuery queryToRun, SearchFilterMessage searchFilterMessage, string stemmingLanguage)
		{
			switch (searchFilterMessage.Operator)
			{
				case SearchFilterOperators.All:
					queryToRun.Add(this.BuildAllQuery(searchFilterMessage, stemmingLanguage), BooleanClause.Occur.MUST);
					break;
				case SearchFilterOperators.Any:
					queryToRun.Add(this.BuildAnyQuery(searchFilterMessage, stemmingLanguage), BooleanClause.Occur.MUST);
					break;
				case SearchFilterOperators.Between:
					queryToRun.Add(this.BuildBetweenQuery(searchFilterMessage), BooleanClause.Occur.MUST);
					break;
				case SearchFilterOperators.Exclude:
					queryToRun.Add(this.BuildExcludeQuery(searchFilterMessage, stemmingLanguage), BooleanClause.Occur.MUST_NOT);
					break;
				case SearchFilterOperators.GreaterEqualThan:
					queryToRun.Add(this.BuildGreaterEqualThanQuery(searchFilterMessage), BooleanClause.Occur.MUST);
					break;
				case SearchFilterOperators.GreaterThan:
					queryToRun.Add(this.BuildGreaterThanQuery(searchFilterMessage), BooleanClause.Occur.MUST);
					break;
				case SearchFilterOperators.LessEqualThan:
					queryToRun.Add(this.BuildLessEqualThanQuery(searchFilterMessage), BooleanClause.Occur.MUST);
					break;
				case SearchFilterOperators.LessThan:
					queryToRun.Add(this.BuildLessThanQuery(searchFilterMessage), BooleanClause.Occur.MUST);
					break;
				case SearchFilterOperators.Like:
					queryToRun.Add(this.BuildLikeQuery(searchFilterMessage, stemmingLanguage), BooleanClause.Occur.MUST);
					break;
				default:
					throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "The search filter operator \"{0}\" is not supported.", searchFilterMessage.Operator));
			}
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <returns></returns>
		protected virtual Query BuildBetweenQuery(SearchFilterMessage searchFilterMessage)
		{
			throw new NotSupportedException();
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <returns></returns>
		protected virtual Query BuildGreaterThanQuery(SearchFilterMessage searchFilterMessage)
		{
			throw new NotSupportedException();
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <returns></returns>
		protected virtual Query BuildGreaterEqualThanQuery(SearchFilterMessage searchFilterMessage)
		{
			throw new NotSupportedException();
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <returns></returns>
		protected virtual Query BuildLessThanQuery(SearchFilterMessage searchFilterMessage)
		{
			throw new NotSupportedException();
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <returns></returns>
		protected virtual Query BuildLessEqualThanQuery(SearchFilterMessage searchFilterMessage)
		{
			throw new NotSupportedException();
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <param name="stemmingLanguage"></param>
		/// <returns></returns>
		protected virtual Query BuildAnyQuery(SearchFilterMessage searchFilterMessage, string stemmingLanguage)
		{
			throw new NotSupportedException();
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <param name="stemmingLanguage"></param>
		/// <returns></returns>
		protected virtual Query BuildAllQuery(SearchFilterMessage searchFilterMessage, string stemmingLanguage)
		{
			throw new NotSupportedException();
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <param name="stemmingLanguage"></param>
		/// <returns></returns>
		protected virtual Query BuildLikeQuery(SearchFilterMessage searchFilterMessage, string stemmingLanguage)
		{
			throw new NotSupportedException();
		}

		/// <summary>
		/// Create query for search filter.
		/// </summary>
		/// <param name="searchFilterMessage"></param>
		/// <param name="stemmingLanguage"></param>
		/// <returns></returns>
		protected virtual Query BuildExcludeQuery(SearchFilterMessage searchFilterMessage, string stemmingLanguage)
		{
			throw new NotSupportedException();
		}
	}
}
